﻿package code
{
	import flash.net.URLLoader;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.errors.IOError;
	
	public class EnemySpawner
	{
		// XML object containing all information of enemy waves
		private var xml:XML;
		// Loader for XML object
		private var xmlLoader:URLLoader;
		// Vector of all the waves of enemies
		private var waveList:Vector.<EnemyWave>;
		// Reference to the level for adding enemies
		private var level:Level;
		// Identifies if XML is loaded and parsed
		private var ready:Boolean;
		// Current enemy wave number player is on
		private var currentWave:int;
		
		// Reference to the level
		public function get CurrentLevel():Level { return level; }
		// Current enemy wave player is on
		public function get CurrentWave():EnemyWave { return waveList[currentWave]; }
		// Identifies if XML is loaded and parsed
		public function get Ready():Boolean { return ready; }
		// Identifies if all enemy waves has been completed
		public function get Completed():Boolean
		{
			if (waveList.length - 1 == currentWave && waveList[currentWave].Completed)
				return true;
			else
				return false;
		}

		public function EnemySpawner(level:Level)
		{
			trace(this + " created.");
			
			this.level = level;
			waveList = new Vector.<EnemyWave>();
			currentWave = 0;
		}
		
		// Loads XML file which contains enemy wave information
		public function Load(url:String)
		{
			xmlLoader = new URLLoader();
			
			xmlLoader.addEventListener(Event.COMPLETE, onXMLLoaded, false, 0 ,true);
			xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, onXMLLoadError, false, 0, true);
			
			xmlLoader.load(new URLRequest(url));
		}
		
		// Parses XML and creates waves and enemies within them
		private function parseXML()
		{
			var waves:XMLList = xml.wave as XMLList;
			
			for (var i:int = 0; i < waves.length(); i++)
			{
				var enemyWave:EnemyWave = new EnemyWave(this);
				
				var singular:XMLList = waves.(@order == i).singular;
				for each (var s:XML in singular.enemy)
				{
					enemyWave.addSingularEnemy(s.attribute("type"), s.attribute("x"),
											   parseInt(s.attribute("y")));
				}
				
				var continuous:XMLList = waves.(@order == i).continuous;
				for each (var c:Object in continuous.enemy)
				{
					enemyWave.addContinuousEnemy(c.attribute("type"), c.attribute("x"),
											   parseInt(c.attribute("y")), parseInt(c.attribute("timestep")));
				}
				
				waveList.push(enemyWave);
			}
			
			ready = true;
		}
		
		// Checks if a wave is a wave is in play and if it has been completed
		public function update()
		{
			if (waveList.length > currentWave)
			{
				if (!waveList[currentWave].Triggered)
				{
					waveList[currentWave].trigger();
					trace("wave triggered");
				}
				else if (waveList[currentWave].Completed)
				{
					currentWave++;
					//waveList.splice(0, 1);
					trace("wave complete");
				}
			}
		}
		
		// Loads XML from file
		private function onXMLLoaded(e:Event)
		{
			xml = new XML(e.target.data);
			
			xmlLoader.removeEventListener(Event.COMPLETE, onXMLLoaded);
			xmlLoader.removeEventListener(IOErrorEvent.IO_ERROR, onXMLLoadError);
			xmlLoader = null;
			
			parseXML();
		}
		
		// Hit when there was an error trying to load the XML file
		private function onXMLLoadError(e:IOError)
		{
			trace("Error loading xml e=" + e);
		}

	}
	
}